// pages/coach/book/book.js
var common = require('../../../utils/common.js');
Page({
  data: {
    index: 0,
    date: '2016-09-01',
    time: '12:01',
    minDate: '2016-09-01',
    minTime: '12:01',
    durations: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    index: 0,
    price: 0,
  },
  id: 0,
  typeName:'',
  coachName:'',
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.id = options.id;
    this.typeName = options.typeName;
    var dt = new Date();
    this.setData({
      date: common.formatDate(dt, 'yyyy-MM-dd'),
      time: common.formatDate(dt, 'hh:mm'),
      minDate: common.formatDate(dt, 'yyyy-MM-dd'),
      minTime: common.formatDate(dt, 'hh:mm'),
      price:options.price
    });
  },
  durationChange: function (e) {
    this.setData({
      index: e.detail.value,
      price:this.data.durations[e.detail.value] * this.data.price
    });
  },
  timeChange: function (e) {
    this.setData({
      time: e.detail.value
    });
  },
  dateChange: function (e) {
    this.setData({
      date: e.detail.value
    });
  },
  formSubmit: function (e) {
   var data = this.extendForm(e.detail.value);
   if(data){
     common.asynPost(common.API.order.coachAdd,
       data,
       function(data){
         wx.redirectTo({
          //  url:common.template('../../pay/bookinginfo/bookinginfo?id={orderId}&name={name}&phone={phone}&orderNo={orderNo}&startTime={startTime}&duration={duration}',data)
          url:'../../pay/bookinginfo/bookinginfo?result=' + JSON.stringify(data['result'])
         });
       }
     );
   }else{
     wx.showToast({
       title:'请填写完整的预约信息',
       mask:true,
       duration:2000
     });
   }
  },
  extendForm:function(frm){
    if(!frm.name){
      return;
    }
    if(!frm.phone){
      return;
    }
    return {
      name:frm.name,
      tel:frm.phone,
      coachId:this.id,
      startTime:this.data.date + " " + this.data.time,
      duration:this.data.durations[this.data.index],
      typeName:this.typeName,
      coachName:this.coachName
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})